<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        class Node {
            constructor(element) {
                this.element = element;//保存元素
                this.next = null;//指向下一个节点
            }
        }

        class LinkedList {
            constructor() {
                this.head = null;
                this.length = 0;
            }

            //append(element): 向列表尾部添加一个新的项
            append(element) {
                //1.根据element创建Node对象
                const newNode = new Node(element);
                //2.追加到最后
                if (!this.head) {//当head为null的时候				this.head === null
                    this.head = newNode;//直接让head等于node节点
                } else {//当head不为null的时候
                    let current = this.head;
                    while (current.next) {
                        current = current.next;		//把head.next赋值给current，循环直到current.next 为空值 把newNode赋值给此时的current.next
                    }
                    current.next = newNode;
                }
                this.length++;
            }
            //insert,在某位置插入某个节点
            insert(position, element) {
                //判断越界问题
                if (position < 0 || position > this.length) return false;
                const newNode = new Node(element);//初始化一个newNode节点
                //越界问题解决后 如果是在首部插入
                if (position === 0) {
                    newNode.next = this.head;
                    this.head = newNode;
                } else {
                    let index = 0;
                    let current = this.head;
                    let previous = null;
                    while (index++ < position) {
                        previous = current;
                        current = current.next;
                    }
                    previous.next = newNode;
                    newNode.next = current;
                }
                this.length++;
                return true;
            }
            //get 获取队友位置的元素
            get(position) {
                //1.判断越界
                if (position < 0 || position > this.length - 1) return null;
                //2.得出值
                let index = 0;
                let current = this.head;
                while (index++ < position) {
                    current = current.next;
                }
                return console.log(current.element);
            }
            //indexOf(element)
            indexOf(element) {
                let index = 0;
                let current = this.head;
                while (current) {
                    if (current.element === element) {
                        return index;
                    }
                    index++;
                    current = current.next;
                }
                return -1;
            }
            //reremoveAt
            removeAt(position) {
                //1.进行越界判断
                if (position < 0 || position > this.length - 1) return null;
                //进行删除
                let index = 0;
                let current = this.head;
                let previous = null;
                if (position === 0) {
                    this.head = current.next;
                } else {
                    while (index++ < position) {
                        previous = current;
                        current = current.next;
                    }
                    previous.next = current.next;
                }
                this.length--;
                return current.element;
            }
            //update(position, element) 
            update(position, element) {
                if (position < 0 || position > this.length - 1 ) return false;
                //删除该位置上的元素
                this.removeAt(position);
                //在该位置里插入元素
                this.insert(position, element);
            }
            //remove(element)
            remove(element) {
                const index = this.indexOf(element);
                this.removeAt(index);
            }
            //isEmpty()
            isEmpty() {
                return this.length === 0;
            }
            //size()
            size() {
                return this.length;
            }

            //自己默写的
            indexOF(element) {
                let index = 0;
                let current = this.head;
                // for(let i = 0; i < this.length; i++) {

                // }
                while(!this.isEmpty()) {
                    if(current.element === element) {
                        return index;
                    } else {
                        current = current.next;
                        index++;
                    }
                }
                return index;
            }

        }
        const linkedList = new LinkedList()
        //append
        linkedList.append("a");
        linkedList.append("b");
        linkedList.append("c");
        linkedList.append("d");
        //insert
        linkedList.insert(1, "d")
        // linkedList.update(1, "c")
        //remove
        // linkedList.remove("c")
        console.log(linkedList);
        //get
        // linkedList.get(0);
        //indexOf
        // console.log(linkedList.indexOf("d"));
        //removeAt
        // linkedList.removeAt(1);
        // console.log(linkedList);
        // console.log(linkedList.indexOF("b"));
    </script>
</body>

</html>